package model

import (
	"engine/consts"
	"github.com/goccy/go-json"
	"gorm.io/gorm"
	"time"
)

type SysUser struct {
	Id           uint64              `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"`
	Email        string              `gorm:"column:email;type:varchar(128);NOT NULL" json:"email"`
	PasswordHash string              `gorm:"column:password;type:varchar(128);NOT NULL" json:"-"`
	Password     string              `gorm:"-" json:"password,omitempty"`
	Nickname     string              `gorm:"column:nickname;type:varchar(32);NOT NULL" json:"nickname"`
	Region       string              `gorm:"column:region;type:varchar(5);default:86;comment:手机号码前缀" json:"region"`
	Mobile       string              `gorm:"column:mobile;type:varchar(16)" json:"mobile"`
	Avatar       string              `gorm:"column:avatar;type:varchar(256)" json:"avatar"`
	RobotId      string              `gorm:"column:robot_id;type:varchar(64)" json:"robotId"`
	Status       consts.CommonStatus `gorm:"column:status;type:enum('active','disable');default:active;NOT NULL" json:"status"`
	DeletedAt    gorm.DeletedAt      `gorm:"column:deleted_at;type:timestamp" json:"deletedAt" swaggertype:"string" extensions:"x-nullable"`
	CreatedAt    time.Time           `gorm:"column:created_at;type:timestamp" json:"createdAt" swaggertype:"string"`
	UpdatedAt    *time.Time          `gorm:"column:updated_at;type:timestamp" json:"updatedAt" swaggertype:"string" extensions:"x-nullable"`
}

func (*SysUser) TableName() string {
	return "sys_users"
}

func (user *SysUser) MarshalBinary() (data []byte, err error) {
	return json.Marshal(user) //解析
}

func (user *SysUser) UnmarshalBinary(data []byte) error {
	return json.Unmarshal(data, user) //反解析
}

type SysUserDetail struct {
	My_user
	Roles []*CoreRole `gorm:"many2many:core_user_has_roles;joinForeignKey:user_id;joinReferences:role_id" json:"roles"`
}
type Xianzhi struct {
	Zhuan    int    `:"zhuan"`
	ZhuanStr string `:"zhuan_str"`
	Zheng    int    `:"zheng"`
	ZhengStr string `:"zheng_str"`
}
type SysUserDetailResp struct {
	Id         uint64  `:"id"`       // ID
	Username   string  `:"username"` // 用户名
	Nickname   string  `:"nickname"` // 昵称
	Mobile     string  `:"mobile"`   // 昵称
	Money      float64 `:"money"`    // 余额
	Score      float64 `:"score"`    // 积分
	Token      string  `:"token"`    // Token
	Q          string  `:"q"`
	Qrcode     string  `:"qrcode"`
	DyTure     int     `:"dy_ture"`
	DyName     string  `:"dy_name"`      // 抖音名称
	DyUsername string  `:"dy_username"`  // 抖音帐号
	AnTaskUrl  string  `:"an_task_url"`  // 抖音帐号
	IosTaskUrl string  `:"ios_task_url"` // 抖音帐号
	IsTask     int     `:"is_task"`      // 抖音帐号
	Xianzhi
	Point         float64 `:"point"` // 抖音帐号
	Jt            int     `:"jt"`    //
	N             int     `:"n"`     //
	T             int     `:"t"`     //
	Zfb           int     `:"zfb"`   //
	Level         int     `:"level"`
	EndDate       int     `:"end_date"`
	OrderNumLevel int     `:"order_num_level"`
	OnlineOrder   bool    `:"online_order"`
	CreateTime    string  `:"create_time"`
	Jhs           int     `:"jhs"`
	Gold          float64 `:"gold"`
	Tz            float64 `name:"tz"`
	WLevel        int64   `:"w_level"`
}

func (user *SysUserDetail) MarshalBinary() (data []byte, err error) {
	return json.Marshal(user) //解析
}

func (user *SysUserDetail) UnmarshalBinary(data []byte) error {
	return json.Unmarshal(data, user) //反解析
}

type CoreRole struct {
	Id          uint64              `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"`
	Name        string              `gorm:"column:name;type:varchar(255);NOT NULL" json:"name"`
	Module      consts.SystemModule `gorm:"column:module;type:varchar(255);NOT NULL" json:"module"`
	Permissions []*CorePermission   `gorm:"many2many:core_role_has_permissions;joinForeignKey:role_id;joinReferences:permission_id" json:"permissions,omitempty"`
	CreatedAt   time.Time           `gorm:"column:created_at;type:timestamp;default:CURRENT_TIMESTAMP;NOT NULL" json:"createdAt" swaggertype:"string"`
	UpdatedAt   *time.Time          `gorm:"column:updated_at;type:timestamp" json:"updatedAt" swaggertype:"string" extensions:"x-nullable"`
}

func (*CoreRole) TableName() string {
	return "core_roles"
}

type CorePermission struct {
	Id        uint64              `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"`
	Name      string              `gorm:"column:name;type:varchar(255);NOT NULL" json:"name"`
	Module    consts.SystemModule `gorm:"column:module;type:varchar(255);NOT NULL" json:"module"`
	CreatedAt time.Time           `gorm:"column:created_at;type:timestamp;default:CURRENT_TIMESTAMP;NOT NULL" json:"createdAt" swaggertype:"string"`
	UpdatedAt *time.Time          `gorm:"column:updated_at;type:timestamp" json:"updatedAt" swaggertype:"string" extensions:"x-nullable"`
}

func (*CorePermission) TableName() string {
	return "core_permissions"
}

type CoreRoleHasPermission struct {
	PermissionId uint64     `gorm:"column:permission_id;type:bigint(20) unsigned" json:"permission_id"`
	RoleId       uint64     `gorm:"column:role_id;type:bigint(20) unsigned;NOT NULL" json:"role_id"`
	CreatedAt    time.Time  `gorm:"column:created_at;type:timestamp;default:CURRENT_TIMESTAMP;NOT NULL" json:"createdAt" swaggertype:"string"`
	UpdatedAt    *time.Time `gorm:"column:updated_at;type:timestamp" json:"updatedAt" swaggertype:"string" extensions:"x-nullable"`
}

func (*CoreRoleHasPermission) TableName() string {
	return "core_role_has_permissions"
}
